
package cn.stylefeng.guns.sys.modular.system.controller;

import cn.hutool.core.io.FileUtil;
import cn.stylefeng.guns.base.auth.context.LoginContext;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.modular.system.entity.BakRecord;
import cn.stylefeng.guns.sys.modular.system.service.BakRecordService;
import cn.stylefeng.guns.sys.modular.system.service.SysQnlKeyService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.SuccessResponseData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * @author YuJiBiao
 * @version: 1.0
 * @Date 2020/7/8 16:30
 **/
@Controller
@RequestMapping("/bak")
public class BakRecordPageController extends BaseController {

    @Autowired
    BakRecordService bakRecordService;
    private String PREFIX = "/modular/system/bak/";

    @Autowired
    LoginContext loginContext;

    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String type) {
        //获取分页参数
        Page<Map<String, Object>> mapPage = bakRecordService.bakRecord(type);
        return LayuiPageFactory.createPageInfo(mapPage);
    }
    /**
     *
     *
     * @author fengshuonan
     * @Date 2018/12/23 4:56 PM
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "bakRecord.html";
    }

    /* 备份数据库 */
    @PostMapping("backupSQL")
    @ResponseBody
    public Object backupSQL() throws IOException {
        long startTime = System.currentTimeMillis();

        String srcPath =System.getProperty("user.home")+"/.qkd";
        String dest =System.getProperty("user.home")+"/bak/.qkd";
        FileUtils.copyDirectory(new File(srcPath),new File(dest));

        long endTime = System.currentTimeMillis();
        BakRecord bakRecord = new BakRecord();
        bakRecord.setFilePath(dest);
        bakRecord.setCreateTime(new Date());
        bakRecord.setTimeConsuming(endTime-startTime);
        bakRecord.setType("备份");
        bakRecord.setUserId(loginContext.getUserId());
        bakRecordService.save(bakRecord);
        return SuccessResponseData.DEFAULT_SUCCESS_CODE;
    }

}
